<template>
  <div>
    <!-- 「营销」支付有礼 -->
    <PayGift
      v-if="payGift.list && payGift.list.length"
      :payGiftData="payGift"
      :isShowAllGifts="isShowAllGifts"
      :maxDisplayGiftsNumber="maxDisplayGiftsNumber"
      @gotoPagePayGift="gotoPagePayGift"
    />
  </div>
</template>

<script>
import * as api from './api';
import PayGift from '@xiaoe/xiaoe-marketing-module/lib/PayGiftBox'
export default {
  name: 'PayGifts',
  components: {
    PayGift, // 「营销」支付有礼
  },
  props: {
    isShowAllGifts: {
      type: Boolean,
      default: false,
    },
    orderInfo: {
      type: Object,
      default: () => {},
    },
    cart: {
      default: 0,
    },
    // aliveInfo:
    isAlive: {
      default:0,
      type:Number
    },
  },
  watch: {
    'orderInfo.order_id': {
      handler(val) {
        setTimeout(() => {
          this.loadPayGift()
        }, 1200)
      }
    }
  },
    created() {
    if(this.isAlive === 1){
      api.changealivePrefix('/_alive')
    }else{
      api.changealivePrefix('')
    }
  },
  data() {
    return {
      payGift: {},
      maxDisplayGiftsNumber: 999,
    };
  },
  methods: {
    // 获取支付有礼内容
    async loadPayGift() {
      this.$emit('loadingStatus', true);
      this.payGift = {};
      let params = { permit_id: this.orderInfo.order_id };
      const { code, data } = await api.getPayGift(params);
      if(code === 0) {
        this.payGift = data;
        this.$emit('loadingStatus', false);
      } else {
        this.$emit('loadingStatus', false);
        throw new Error('服务器报错啦');
      }
    },
    gotoPagePayGift() {
      this.$router.push({
        path: '/pay_gift',
        query: {
          order_id: this.orderInfo.order_id,
          cart: this.cart,
        },
      });
    },
  },
};
</script>

<style></style>
